﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Configuration;
using System.IO;
using LeadVinaglue.Models.ViewPages;
using LeadVinaglue.Repository;
using LeadVinaglue.Models;
using LeadVinaglue.Helpers;
using LeadVinaglue.Utility;
namespace LeadVinaglue.Controllers
{
    public class DangtinController : Controller
    {
        // Create an instance of user manage
        LeadVinaglue.Repository.LEManagementRepository repository = new LeadVinaglue.Repository.LEManagementRepository();

        // GET: /Management/Home/
        [DeviceFilterAttribute(Order = 1)]
        public ActionResult Index()
        {
            LEManagementModel model = new LEManagementModel();
            model = repository.GetProductUnknow(LeadConstant.UNKNOW);

            return View(model);
        }

        [HttpPost]
        [ValidateInput(false)]
        [Authorize(Roles = "ClientAdmin")]
        public ActionResult Index(FormCollection form)
        {
            ProductManagementRepository pmRepository = new ProductManagementRepository();
            var user = new Repository.UserRepository().GetSingle(User.Identity.Name);
            // Create product
            Product model = new Product();

            model.GUID = Guid.NewGuid().ToString();
            model.Title = form["Title"].ToString();
            model.ShortDesc = form["ShortDesc"].ToString();
            if (!string.IsNullOrEmpty(model.ShortDesc) && model.ShortDesc.Length > 255)
            {
                model.ShortDesc = model.ShortDesc.Substring(0, 255);
            }
            model.LongDesc = form["LongDesc"].ToString();
            model.CategoryID = (form["CategoryID"] != null) ? int.Parse(form["CategoryID"].ToString()) : 0;
            model.SubCategoryID = (form["SubCategoryID"] != null) ? int.Parse(form["SubCategoryID"].ToString()) : 0;
            model.CreatedByUserID = 0;
            model.Active = false;

            // Transaction
            int NumOfDay = int.Parse(ConfigurationManager.AppSettings["NumOfDay"].ToString());
            model.StartDate = DateTime.Now;
            model.EndDate = DateTime.Now.AddDays(Convert.ToInt32(NumOfDay));
            pmRepository.Create(model);

            // ViewModel
            LEManagementModel viewModel = new LEManagementModel();
            viewModel = repository.GetProductUnknow(LeadConstant.UNKNOW);
            ViewBag.Messages = "Bạn vừa thêm mới tài liệu thành công";

            return View(viewModel);

        }
    }
}
